/* ��ġ�� �ִϸ��̼� 13-02-04
 * START ��ư�� ������ �ΰ��� ����� �ִϸ��̼� (������, Ʈ��)�� ���ÿ� ���۽�Ű�� ���ÿ� �����Ų��*/
package com.example.ddakji;

import android.app.Activity;
import android.os.Handler;
import android.util.Log;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageSwitcher;

public class Animation_Class extends Activity{

    private static final String TAG = "Animation_Class";
	ImageSwitcher switcher; 
	Handler mHandler = new Handler();
	ImageThread thread; //������ �ִϸ��̼��� ������ ������
	
	boolean running; 
    
	boolean isRunning;


    
    public void Click_Event(ImageSwitcher switcher_put,Handler vHandler){
    	mHandler = vHandler;
    	Log.e(TAG, "++ Click_Event ++");
    	Log.e(TAG, "++ Click_Event01 ++");
          		switcher = switcher_put;
            	Log.e(TAG, "++ Click_Event001 ++");
           		 Animation anim_up = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scaleup);
             	Log.e(TAG, "++ Click_Event0001 ++");
           	     final Animation anim_down = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.scaledown); //Ŀ���� �۾����� Ʈ�� �ִϸ��̼�

             	Log.e(TAG, "++ Click_Event00001 ++");
           	     anim_up.setInterpolator(new DecelerateInterpolator()); //Ʈ�� �ִϸ��̼� �ӵ� ����
            		 anim_down.setInterpolator(new AccelerateInterpolator());
           		startAnimation();	//������ �ִϸ��̼� ����
            	Log.e(TAG, "++ Click_Event1 ++");
           		
           		switcher.startAnimation(anim_up); //Ŀ���� Ʈ��  �ִϸ��̼� ���� 
           		anim_up.setAnimationListener(new AnimationListener(){

           		public void onAnimationEnd(Animation _animation) {
           			switcher.startAnimation(anim_down);	//Ŀ���� Ʈ�� �ִϸ��̼� �����ϸ� �۾����� Ʈ�� �ִϸ��̼� ����
           		}
           		public void onAnimationRepeat(Animation _animation) {		}
           		public void onAnimationStart(Animation _animation) { 		}		
           		});

       	    	Log.e(TAG, "++ Click_Event2 ++");
           		anim_down.setAnimationListener(new AnimationListener(){
           			public void onAnimationEnd(Animation _animation) {
           				 stopAnimation(); 	//�۾����� Ʈ�� �ִϸ��̼� �����ϸ� �����Ӿִϸ��̼� ��������
               		}
               		public void onAnimationRepeat(Animation _animation) {		}
               		public void onAnimationStart(Animation _animation) { 		}		
               		});
           		

       	    	Log.e(TAG, "++ Click_Event3 ++");
  
    }
    
    
    private void startAnimation()	//������ �ִϸ��̼� ����
    { 
	    	Log.e(TAG, "++ Click_Event4 ++");
    	thread = new ImageThread();
    	thread.start();	//������ ����
    }
    
    
    private void stopAnimation() //������ �ִϸ��̼� ����
    {
    	running = false;
    	try {
    		thread.join(); //������ ������� ���
    	} catch(InterruptedException ex){}
    	//switcher.setVisibility(View.INVISIBLE);
    }
    
    class ImageThread extends Thread {
    	
    	float duration = 10; //ù ������ ���۰�
    	
    	final int imageId[] = { R.drawable.ddak1,
    			R.drawable.ddak2,
    			R.drawable.ddak3,
    			R.drawable.ddak4,
    			R.drawable.ddak5,
    			R.drawable.ddak6,
    			R.drawable.ddak7,
    			R.drawable.ddak8,
    			R.drawable.ddak9,
    			R.drawable.ddak10,
    			R.drawable.ddak11,
    			R.drawable.ddak12,
    			R.drawable.ddak13,
    			R.drawable.ddak14,
    			R.drawable.ddak15,
    			R.drawable.ddak16,
    			R.drawable.ddak17,
    			R.drawable.ddak18}; //���� �ѹ��� ���� ������
    	int currentIndex = 0;
    	 
    	public void run(){
    		running = true;
    		while(running) {
    			synchronized(this){
    				mHandler.post(new Runnable() {
    					public void run(){
    						
    						switcher.setImageResource(imageId[currentIndex]);
    						duration+=100/81;
    					}
    				});
    				currentIndex++;
    				
    				if(currentIndex == imageId.length){
    					currentIndex = 0;
    				}
    				
    				
    				try {
    					Thread.sleep((int)duration);
    				} catch (InterruptedException ex){}
    				
    				/*if (count == 90) {
    					count = 0;
    		    		stopAnimation();
    		    		
    				}*/
    			}
    		}
    	}
    }

    
}
/* ��ġ�� �ִϸ��̼� 13-02-04*/